﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HuoTu.Common;

using System.Reflection;

using System.Threading;


namespace DomainSearchTools.Common
{
    /// <summary>
    /// 批量搜索注册情况
    /// </summary>
    public class DomainSearchHelper
    {
        private const string ServerUrl = @"http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=";
        //private const string RequestUrlB = @"http://whois.www.net.cn/whois/api_whois_full?host=teapic.com&web_server=";
        //private const string RequestUrlC = @"http://reports.internic.net/cgi/whois?whois_nic=com&type=domain";
        //private List<string> domainList;
        /// <summary>
        /// 待搜索域名列表
        /// </summary>
        //public List<string> DomainList
        //{
        //    get { return domainList; }
        //    set { domainList = value; }
        //}


        //private List<DomainResultModel> searchResult;
        ///// <summary>
        ///// 搜索结果列表
        ///// </summary>
        //public List<DomainResultModel> SearchResult
        //{
        //    get { return searchResult; }
        //    set { searchResult = value; }
        //}

        //private string currentDomain;
        ///// <summary>
        ///// 当前待搜索的域名
        ///// </summary>
        //public string CurrentDomain
        //{
        //    get { return currentDomain; }
        //    set { currentDomain = value; }
        //}

        //private DomainStatus currentResult;
        ///// <summary>
        ///// 当前的搜索结果
        ///// </summary>
        //public DomainStatus CurrentResult
        //{
        //    get { return currentResult; }
        //}

        //private SearchStatus status;
        ///// <summary>
        ///// 搜索的状态
        ///// </summary>
        //public SearchStatus Status
        //{
        //    get { return status; }
        //    set { status = value; }
        //}

        //private Thread thread = null;
        //public DomainSearchHelper(List<string> domainList)
        //{
        //    this.domainList = domainList;
        //}

        private int searchSpeed = 500;
        /// <summary>
        /// 搜索速度（默认500毫秒）
        /// </summary>
        public int SearchSpeed
        {
            get { return searchSpeed; }
            set { searchSpeed = value; }
        }
        /// <summary>
        /// 按顺序搜索
        /// </summary>
        //private int searchCount;
        ///// <summary>
        ///// 按顺序搜索
        ///// </summary>
        //public int SearchCount
        //{
        //    get { return searchCount; }
        //    set { searchCount = value; }
        //}

        private System.Timers.Timer time = new System.Timers.Timer()
        {
            Interval = 1000,
            AutoReset = true,
            Enabled = false
        };
        /// <summary>
        /// 定时搜索时间
        /// </summary>
        public System.Timers.Timer Time
        {
            get { return time; }
            set { time = value; }
        }

        private DomainPushHelper push;
        /// <summary>
        /// 用于查询推入状态
        /// </summary>
        public DomainPushHelper Push
        {
            get { return push; }
            set { push = value; }
        }

        
        /// <summary>
        /// 开始处理搜索列表
        /// </summary>
        public void StartSearch()
        {
            time.Elapsed += t_Elapsed;
            time.Enabled = true;
        }

        private Mutex mx = new Mutex();
        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //System.Timers.Timer tt = (System.Timers.Timer)sender;
            //tt.Enabled = false;
            mx.WaitOne();
            DoSearch();
            mx.ReleaseMutex();
            //tt.Enabled = true;
        }

        /// <summary>
        /// 观察池中数据变化 进行搜索
        /// </summary>
        private void DoSearch()
        {
            bool allow = false;
            int index = 0;
            int poolCount = DomainPoolHelper.List.Count;
            int searchPoolCount = DomainSearchPoolHelper.List.Count;
            if (searchPoolCount == 0 && poolCount > 0)
            {
                allow = true;
                index = 0;
            }
            else if (searchPoolCount < poolCount)
            {
                allow = true;
                index = searchPoolCount;
            }
            //else if (push.Status == PushStatus.FINISHED && searchCount == poolCount - 1)
            //{
            //    allow = true;
            //    index = poolCount - 1;
            //}

            if (allow)
            {
                try
                {
                    DomainPoolModel pool = DomainPoolHelper.List[index];
                    SearchWebThread web = new SearchWebThread(ServerUrl + pool.DomainName, index, pool.DomainName);
                    Thread thread = new Thread(new ThreadStart(web.DoWebResponse));
                    thread.Start();
                    //阻塞调用线程 等待线程结束
                    thread.Join();

                    //Thread.Sleep(searchSpeed);
                }
                catch
                {
                    //break;
                }
            }
        }

        public class SearchWebThread
        {
            private string webUrl;

            //public string WebUrl
            //{
            //    get { return webUrl; }
            //    set { webUrl = value; }
            //}

            private int poolIndex;

            //public int PoolIndex
            //{
            //    get { return poolIndex; }
            //    set { poolIndex = value; }
            //}

            //private string result;
            ///// <summary>
            ///// 返回搜索的结果
            ///// </summary>
            //public string Result
            //{
            //    get { return result; }
            //}
            private string domainName { get; set; }
            /// <summary>
            /// 域名后缀
            /// </summary>
            //private string suffix { get; set; }

            public SearchWebThread(string webUrl, int poolIndex, string domainName)
            {
                this.webUrl = webUrl;
                this.poolIndex = poolIndex;
                this.domainName = domainName;
                //this.suffix = suffix;
            }

            public void DoWebResponse()
            {
                string result = SysHtml.HttpWebResponse(this.webUrl);

                DomainStatus status = result.IndexOf(((int)DomainStatus.NOREGED).ToString()) >= 0 ? DomainStatus.NOREGED : DomainStatus.REGED;
                //DomainPoolHelper.List[poolIndex].IsSearched = true;
                //DomainPoolHelper.List[poolIndex].Status = status;
                DomainSearchPoolHelper.List.Add(new DomainPoolModel()
                {
                    DomainName = domainName,
                    IsSearched = true,
                    Status = status,
                    //Suffix = suffix
                });

                //searchCount++;
            }
        }
    }
}